<template>
  <div>
    <el-button slot="reference" @click="showCompleted">维修任务已处理</el-button>
    <el-button slot="reference" @click="showUncompleted">维修任务未处理</el-button>
    <el-table :data="tableData">
      <el-table-column prop="createdTime" label="日期" width="100" :formatter="formatDate">
      </el-table-column>
      <el-table-column prop="studentName" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="buildingName" label="楼栋" width="100">
      </el-table-column>
      <el-table-column prop="roomName" label="房间" width="100">
      </el-table-column>
      <el-table-column prop="reason" label="原因" width="100">
      </el-table-column>
      <el-table-column prop="keeper" label="维修人员" width="100">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100">
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
              v-if="isCompleted"
              size="small"
              type="success"
              disabled>
            已完成
          </el-button>
          <el-button
              v-else
              size="small"
              type="primary"
              @click="handleAllocation(scope.row)">
            完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total=totalNum>
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {getUndoRepair, updateRepairKeeper, getDoneRepair} from '../../../api/keeper'
export default {
  data () {
    return {
      pageNo: 1,
      pageSize: 5,
      totalNum: 0,
      dialogTableVisible: false,
      dialogFormVisible: false,
      DOT: {
        pageNo: 1,
        pageSize: 5,
        data: {
          ID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
      },
      keepers: [],
      isCompleted: true,
      selectForm: {
        building: '',
        inRoom: ''
      },
      allocationForm: {
        id: '',
        createdTime: '',
        studentName: '',
        buildingName: '',
        roomName: '',
        reason: '',
        keeper: null
      },
      tableData: []
    }
  },
  methods: {
    formatDate (row) {
      const date = new Date(row.createdTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    handleCurrentChange (newPage) {
      this.DOT.pageNo = newPage
      this.pageNo = newPage
      if (this.isCompleted) {
        this.getCompleted()
      } else {
        this.getUncompleted()
      }
    },
    showCompleted () {
      this.isCompleted = true
      this.DOT.pageNo = 1
      this.pageNo = 1
      this.getCompleted()
    },
    getCompleted () {
      getDoneRepair(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    showUncompleted () {
      this.DOT.pageNo = 1
      this.pageNo = 1
      this.totalNum = 0
      this.isCompleted = false
      this.getUncompleted()
    },
    getUncompleted () {
      getUndoRepair(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    handleAllocation (row) {
      let obj = {
        ID: row.id.toString()
      }
      updateRepairKeeper(obj).then(res => {
        if (res.status) {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
          this.showUncompleted()
        } else {
          this.$message({
            message: '更新失败',
            type: 'error'
          })
        }
      })
    }
  },
  created () {
    this.showCompleted()
  }
}
</script>
<style scoped>
</style>
